package lab03.Test13;

import java.sql.*;

/**
 * Created by Administrator on 2017/11/24.
 */
public class Test13 {
    public static void main(String[] args) {

        Connection connection =null;
        Statement statement = null;
        ResultSet resultSet = null;

        String inesert = "insert into " +
                "t_students(real_name,age,gender,department,stu_no)" +
                " values('san',20,0,'信息与工程学院','1606101092')";//插入数据10条，
        String delete = "DELETE  from t_students WHERE id = 5";//删除数据
        String select = "select * from t_students ";//查询数据
        String update = "update t_students " +
                "set department='信息与工程学院' " +
                "where id = 3 ";//更新数据
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动程序

            connection = DriverManager.getConnection(
              "jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",//数据库链接字符串
                   "root",
                    "123456"
            );
            statement = connection.createStatement();
            statement.execute(inesert);//执行插入数据
            statement.execute(update);//执行更新数据
            statement.execute(delete);//执行删除数据

            //ResultSet :"查"才会有结果，增删改不会有
            resultSet = statement.executeQuery(select);//执行查询数据
            while(resultSet.next()){//返回的是布尔值，用于判断是否有结果，如有就取得对应的key和value。
                System.out.print(resultSet.getString("id") + " ");
                System.out.print(resultSet.getString("stu_no") + " ");
                System.out.print(resultSet.getString("real_name") + " ");
                System.out.print(resultSet.getString("gender") + " ");
                System.out.println(resultSet.getString("department") + " ");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
}
